

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class YKNetworkImageWidget extends StatelessWidget {
  final String url;
  final double? width;
  final double? height;
  final double? radius;
  final BoxFit? fit;
  final Widget? placeholder;

  const YKNetworkImageWidget({super.key, required this.url, this.width, this.height, this.radius = 0, this.fit, this.placeholder});

  @override
  Widget build(BuildContext context) {
    if (url.isEmpty) {
      return SizedBox(width: width, height: height);
    }
    if (radius == 0) {
      return CachedNetworkImage(
        imageUrl: url,
        width: width,
        height: height,
        fit: fit,
        placeholder: (context, url) => placeholder != null ? placeholder! : const SizedBox(),
        errorWidget: (context, url, error) =>
            Container(color: Colors.grey, width: width, height: height, child: const Icon(Icons.error, color: Colors.white)),
      );
    }
    return CachedNetworkImage(
      imageUrl: url,
      width: width,
      height: height,
      fit: fit,
      imageBuilder: (context, imageProvider) => ClipRRect(
        borderRadius: BorderRadius.circular(radius ?? 0),
        child: Image(image: imageProvider),
      ),
      placeholder: (context, url) => placeholder != null ? placeholder! : const SizedBox(),
      errorWidget: (context, url, error) =>
          Container(color: Colors.grey, width: width, height: height, child: const Icon(Icons.error, color: Colors.white)),
    );
  }
}
